<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body onload="readCookie()">
<form name="myform">
    用户名：<input type="text" name="username">
    密码：<input type="password" name="pwd">
    <input type="button" name="sub" value="登录" onclick="login()">
</form>
<script type="text/javascript">
    function login(){
        var uname=myform.username.value;
        var pwd=myform.pwd.value;
        if(uname.length==0){
            alert("用户名不可为空");
            return false;
        }else if(uname!="mr"){
            alert("用户名不正确");
            return false;
        }else if(pwd.length==0){
            alert("密码不可为空");
            return false;
        }else if(pwd!="111"){
            alert("密码不正确");
            return false;
        }else{
            alert("登录成功");
            writeCookie();
            readCookie();
        }
    }
    function writeCookie(){
        document.cookie=encodeURI("username="+myform.username.value);
        document.cookie=encodeURI("password="+myform.pwd.value);
    }
    function readCookie(){
        var cookieString=decodeURI(document.cookie);
        var cookieArray=cookieString.split(";");
        for(var i=0;i<cookieArray.length;i++){
            var cookieNum=cookieArray[i].trim().split("=");
            if(cookieNum[0]=="username") myform.username.value=cookieNum[1];
            if(cookieNum[0]=="password") myform.pwd.value=cookieNum[1];
        }
    }

</script>
</body>
</html>